<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib prefix="sys" uri="/WEB-INF/tld/sys.tld" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>详情页面</title>
    <style type="text/css">
        .searchArea .fieldBox .label {
            width: 70px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
    </style>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>

<body class="iframeBody">

<div class="Wrapper">
    <div class="searchArea">
        <form id="searchBar">
            <div class="fieldset1 clearFix">
                <div class="fieldBox">
                    <div class="label">供应商名称</div>
                    <div class="field">
                        <input name="splNm" id="splNm" type="text" class="ui-textbox" value="">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">所属事业部</div>
                    <div class="field">
                        <select name="buOrgCd" id="buOrgCd" data-value=" ">
                            <option value=" " seleted>请选择</option>
                            <c:forEach items="${searchBuOrgList }" var="kv">
                                <option value="${kv.key }">${kv.value }</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">是否生效</div>
                    <div class="field">
                        <select name="isValid" id="isValid" data-value=" ">
                            <option value="9" seleted>全部</option>
                            <option value="1">是</option>
                            <option value="0">否</option>
                        </select>
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">添加日期</div>
                    <div class="field">
                        <input ligerui="width: 120" name="crtTmStar"
                               id="crtTmStar" type="text" class="ui-datepicker" value=""
                               validate="{required:true}"> - <input
                            ligerui="width: 120" name="crtTmEnd" id="crtTmEnd"
                            type="text" class="ui-datepicker" value=""
                            validate="{required:true}">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">生效日期</div>
                    <div class="field">
                        <input ligerui="width: 120" name="offectTmStar"
                               id="offectTmStar" type="text" class="ui-datepicker" value=""
                               validate="{required:true}"> - <input
                            ligerui="width: 120" name="offectTmEnd" id="offectTmEnd"
                            type="text" class="ui-datepicker" value=""
                            validate="{required:true}">
                    </div>
                </div>
                <div class="fieldBox">
                    <div class="label">失效日期</div>
                    <div class="field">
                        <input ligerui="width: 120" name="efficacyTmStar"
                               id="efficacyTmStar" type="text" class="ui-datepicker" value=""
                               validate="{required:true}"> - <input
                            ligerui="width: 120" name="efficacyTmEnd" id="efficacyTmEnd"
                            type="text" class="ui-datepicker" value=""
                            validate="{required:true}">
                    </div>
                </div>

                <div class="clearFix"></div>
                <div class="formBtns notRight">
                    <span id="searchBtn" class="sq-button sq-button-round"></i>查询</span>
                    <span id="clearBtn" class="sq-button sq-button-round"></i>清空</span>
                    <c:if test="${isCanAdd == 1}">
                        <span id="addBtn" class="sq-button sq-button-round"></i>新增</span>
                    </c:if>
                    <input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
                </div>
            </div>
        </form>
    </div>

    <div id="ligergrid1" class="sq-grid"></div>
</div>

<script type="text/javascript">
    var grid = null;
    var callback = null;

    $(function () {
        grid = $("#ligergrid1").ligerGrid({
            url: '<%=path%>/spl/rebates/initRebateRulesList.do',
            parms: {},
            method: 'get',
            columns: [
                {display: '供应商名称', name: 'splNm', width: '15%', align: 'center', enabledSort: false},
                {display: '所属事业部', name: 'buOrgCdNm', width: '15%', align: 'center', enabledSort: false},
                {display: '添加日期', name: 'crtTm', width: '15%',render: renderDate, align: 'center', enabledSort: false},
                {display: '生效日期', name: 'indateStar', width: '15%',render: renderDate2, align: 'center', enabledSort: false},
                {display: '失效日期', name: 'indateEnd', width: '15%',render: renderDate2, align: 'center', enabledSort: false},
                {display: '是否大客户业务', name: 'isVipWork', width: '5%', align: 'center', enabledSort: false,
                    render: function (rowdata, rowindex, value){
                        if(StringUtils.isNotEmpty(rowdata.isVipWork) && rowdata.isVipWork == 1){
                            return  "是";
                        }else{
                            return '否';
                        }
                    }},
                {display: '是否生效', name: 'isValid', width: '5%', align: 'center', enabledSort: false,
                    render: function (rowdata, rowindex, value){
                        if(StringUtils.isNotEmpty(rowdata.isValid) && rowdata.isValid == 1){
                            return  "是";
                        }else{
                            return '否';
                        }
                    }},
                {display: '添加人', name: 'crtUsrNm', width: '10%', align: 'center', enabledSort: false},
                {display: '操作', name: '', width: '15%', align: 'center', enabledSort: false,
                    render: function (rowdata, index, value) {
                        var str ='';
                        var b='<a class="sq-button-hasicon" href="javascript:tomodify(';
                        b+="'"+rowdata.id+"'";
                        b+=')"><i class="sqBtnIco_22"></i>修改</a>';
                        if(rowdata.isDel == 0) {
                            str += b;
                        }
                        var e='<a class="sq-button-hasicon" href="javascript:modifyIsvalid(';
                        e+="'"+rowdata.id+"'";
                        e+=')"><i class="sqBtnIco_22"></i>禁用</a>';
                        if(rowdata.isValid == 1){
                            str+=e;
                        }
                        return str;
                    }
                }
            ],
            pageSize: 10,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: '100%',
            heightDiff: -2,
            checkbox: false,
            rownumbers: true,
            allowUnSelectRow: true,
            alternatingRow: true,
            frozen: false
        });
        grid.setHeight(500);

        $("#searchBar").ligerForm();

        /* 查询 */
        $("#searchBtn").click(function (event) {
            var form = liger.get("searchBar");
            var data = form.getData();
            for (name in data) {
                grid.setParm(name, data[name]);
            }
            var crtTmMonth = MonthsBetw(data['crtTmStar'],data['crtTmEnd'])
            if(crtTmMonth > 12){
                $.ligerDialog.SQalert({content: "添加日期不能超过12个月！", SQ_type: "error"});
                return ;
            }
            var crtTmMonth = MonthsBetw(data['offectTmStar'],data['offectTmEnd'])
            if(crtTmMonth > 12){
                $.ligerDialog.SQalert({content: "生效日期不能超过12个月！", SQ_type: "error"});
                return ;
            }
            var crtTmMonth = MonthsBetw(data['efficacyTmStar'],data['efficacyTmEnd'])
            if(crtTmMonth > 12){
                $.ligerDialog.SQalert({content: "失效日期不能超过12个月！", SQ_type: "error"});
                return ;
            }

            grid.reload();
        });

        /* 清空 */
        $("#clearBtn").click(function (event) {
            $("#resetBtn").click();  //重置 input-text
            var form = liger.get("searchBar");
            form.setData({  //重置select等特殊表单
                "splNm": " ",
                "buOrgCd": "",
                "isValid" :"9",
                "crtTmStar":"",
                "crtTmEnd":"",
                "offectTmStar":"",
                "offectTmEnd":"",
                "efficacyTmStar":"",
                "efficacyTmEnd":""
            });
        });
        callback = function () {
            grid.reload();
        };

    });
    function MonthsBetw(date1, date2) { //date1和date2是2019-3-12格式
        if(!date1 && !date2){
            return 0 ;
        }
        if(date2 && !date1){
            $.ligerDialog.SQalert({content: "查询起始时间为空！！", SQ_type: "error"});
        }
        if(date1 && !date2){
            $.ligerDialog.SQalert({content: "查询结束时间为空！", SQ_type: "error"});
        }
        date1 = date1.split("-");
        date2 = date2.split("-");
        //获取年,月数
        var year1 = parseInt(date1[0]),
            month1 = parseInt(date1[1]),
            year2 = parseInt(date2[0]),
            month2 = parseInt(date2[1]),
            //通过年,月差计算月份差
            months = (year2 - year1) * 12 + (month2 - month1) + 1;
        return months;
    }

    function getOptColumns() {
        var col = {
            display: '操作',
            name: '',
            width: '15%',
            align: 'center',
            enabledSort: false,
            render: function (rowdata, index, value) {
                var str ='';
                var b='<a class="sq-button-hasicon" href="javascript:tomodify(';
                b+="'"+rowdata.id+"'";
                b+=')"><i class="sqBtnIco_22"></i>修改</a>';
                if(rowdata.isDel == 0) {
                    str += b;
                }
                var c='<a class="sq-button-hasicon" href="javascript:modifyIsvalid(';
                c+="'"+rowdata.id+"'";
                c+=')"><i class="sqBtnIco_22"></i>禁用</a>';
                if(rowdata.isValid == 1){
                    str+=c;
                }
                return str;
            }
        };
        return col;
    }
    //格式化时间
    function renderDate(rowdata, index, value){
        var dateStr = "";
        if(null != value){
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
        }
        return dateStr;
    }

    //格式化时间
    function renderDate2(rowdata, index, value){
        var dateStr = "";
        if(null != value){
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }
    function modifyIsvalid(_id) {
        if(!_id)return;
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: {id:_id},
            url: '<%=path%>/spl/rebates/modifyIsvalid.do',
            beforeSend: function () {
                wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
            },
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                    grid.reload();
                } else {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            }
        });
    }
    function tomodify(_id) {
        var onCancel = function (item, Dialog) {
            Dialog.hide();
        };
        $.ligerDialog.SQopen({
            width: 1000,
            //height: 800,
            autoFixHeight: true,
            title: '修改',
            url: '<%=path%>/spl/rebates/toModifyPage.do?id='+_id,
            buttons: [
                {
                    text: "关闭", onclick: onCancel, type: 'cancel'
                }
            ]
        });
    }
    function dialogCallback() {
        $.ligerDialog.hide();
        grid.reload();
    }

</script>

</body>
</html>
